package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.ComposeShader
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.SweepGradient
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SetLastPointView
* */

/*
    * PorterDuff作用场景:在原有图像上绘制其他内容

      PorterDuff.Mode为枚举类,一共有16个枚举值:
      1.PorterDuff.Mode.CLEAR
      所绘制不会提交到画布上。
      *
      2.PorterDuff.Mode.SRC
      显示上层绘制图片
      *
      3.PorterDuff.Mode.DST
      显示下层绘制图片
      *
      4.PorterDuff.Mode.SRC_OVER
      正常绘制显示，上下层绘制叠盖。
      *
      5.PorterDuff.Mode.DST_OVER
      上下层都显示。下层居上显示。
      *
      6.PorterDuff.Mode.SRC_IN
      取两层绘制交集。显示上层。
      *
      7.PorterDuff.Mode.DST_IN
      取两层绘制交集。显示下层。
      *
      8.PorterDuff.Mode.SRC_OUT
      取上层绘制非交集部分。
      *
      9.PorterDuff.Mode.DST_OUT
      取下层绘制非交集部分。
      *
      10.PorterDuff.Mode.SRC_ATOP
      取下层非交集部分与上层交集部分
      *
      11.PorterDuff.Mode.DST_ATOP
      取上层非交集部分与下层交集部分
      *
      12.PorterDuff.Mode.XOR
      异或：去除两图层交集部分
      *
      13.PorterDuff.Mode.DARKEN
      取两图层全部区域，交集部分颜色加深
      *
      14.PorterDuff.Mode.LIGHTEN
      取两图层全部，点亮交集部分颜色
      *
      15.PorterDuff.Mode.MULTIPLY
      取两图层交集部分叠加后颜色
      *
      16.PorterDuff.Mode.SCREEN
      取两图层全部区域，交集部分变为透明色
    * */

class FourShaderView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
        }
    }

    private val mPaint3 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
        }
    }

    private var mLinearGradient: LinearGradient? = null
    private var mRadialGradient:  RadialGradient? = null
    private var mSweepGradient:  SweepGradient? = null
    private var mComposeShader:  ComposeShader? = null

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            if (mLinearGradient == null) {
                mLinearGradient = LinearGradient(
                    -cx / 2,
                    -cy / 2 - cx / 2 * 5 / 6,
                    -cx / 2,
                    cy / 2 + cx / 2 * 5 / 6,
                    intArrayOf(
                        context.resources.getColor(R.color.color_FFEA00),
                        context.resources.getColor(R.color.color_FFC400),
                        context.resources.getColor(R.color.color_EA992E)
                    ),
                    floatArrayOf(0.0F, 0.45F, 1.0F),
                    Shader.TileMode.CLAMP
                )
            }
            mPaint0.shader = mLinearGradient
            drawCircle(
                -cx / 2,
                -cy / 2,
                cx / 2 * 5 / 6,
                mPaint0
            )

            if (mRadialGradient == null) {
                mRadialGradient = RadialGradient(
                    cx / 2,
                    -cy / 2,
                    cx / 2 * 5 / 6,
                    context.resources.getColor(R.color.color_A4F85D),
                    context.resources.getColor(R.color.color_BAED07),
                    Shader.TileMode.CLAMP
                )
            }
            mPaint1.shader = mRadialGradient
            drawCircle(
                cx / 2,
                -cy / 2,
                cx / 2 * 5 / 6,
                mPaint1
            )

            if (mSweepGradient == null) {
                mSweepGradient = SweepGradient(
                    cx / 2,
                    cy / 2,
                    intArrayOf(
                        context.resources.getColor(R.color.color_00796B),
                        context.resources.getColor(R.color.color_388E3C),
                        context.resources.getColor(R.color.color_689F38)
                    ),
                    floatArrayOf(0.0F, 0.75F, 1.0F)
                )
            }
            mPaint2.shader = mSweepGradient
            drawCircle(
                cx / 2,
                cy / 2,
                cx / 2 * 5 / 6,
                mPaint2
            )

            if (mComposeShader == null) {
                mComposeShader =
                    ComposeShader(mRadialGradient!!, mLinearGradient!!, PorterDuff.Mode.DST_IN)
            }
            mPaint3.shader = mComposeShader
            drawCircle(
                -cx / 2,
                cy / 2,
                cx / 2 * 5 / 6,
                mPaint3
            )

            restore()
        }
    }
}